using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The TeacherContactTableDB class is responsible for interacting with the database to retrieve and store information
/// about TeacherContactTable objects.
/// </summary>
public class TeacherContactTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TeacherContactTable from the underlying datasource.
/// </summary>
/// <param name="teacherContactKey">The unique TeacherContactKey of the TeacherContactTable in the database.</param>
/// <returns>An TeacherContactTable when the TeacherContactKey was found in the database, or null otherwise.</returns>
public static TeacherContactTable GetItem(int teacherContactKey)
{
TeacherContactTable myTeacherContactTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@teacherContactKey", teacherContactKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTeacherContactTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTeacherContactTable;
}
}

public static TeacherContactTable GetItemByUserAccountKey(int UserAccountKey)
{
    TeacherContactTable myTeacherContactTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myTeacherContactTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myTeacherContactTable;
    }
}

/// <summary>
/// Returns a list with TeacherContactTable objects.
/// </summary>
/// <returns>A generics List with the TeacherContactTable objects.</returns>
public static TeacherContactTableList GetList()
{
TeacherContactTableList tempList = new TeacherContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with TeacherContactTable objects.
/// </summary>
/// <returns>A generics List with the TeacherContactTable objects.</returns>
public static TeacherContactTableList GetListByUserAccountKey(int UserAccountKey)
{
TeacherContactTableList tempList = new TeacherContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a TeacherContactTable in the database.
/// </summary>
/// <param name="myTeacherContactTable">The TeacherContactTable instance to save.</param>
/// <returns>The new TeacherContactKey if the TeacherContactTable is new in the database or the existing TeacherContactKey when an item was updated.</returns>
public static int Save(TeacherContactTable myTeacherContactTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTeacherContactTable.TeacherContactKey == -1)
{myCommand.Parameters.AddWithValue("@teacherContactKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@teacherContactKey", myTeacherContactTable.TeacherContactKey);
}
if (myTeacherContactTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myTeacherContactTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myTeacherContactTable.MobileNumber))
{
myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@mobileNumber", myTeacherContactTable.MobileNumber);
}
if (string.IsNullOrEmpty(myTeacherContactTable.HomePhoneNumber))
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", myTeacherContactTable.HomePhoneNumber);
}
if (string.IsNullOrEmpty(myTeacherContactTable.OtherNumber))
{
myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@otherNumber", myTeacherContactTable.OtherNumber);
}
if (string.IsNullOrEmpty(myTeacherContactTable.OfficePhoneNumber))
{
    myCommand.Parameters.AddWithValue("@OfficePhoneNumber", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@OfficePhoneNumber", myTeacherContactTable.OfficePhoneNumber);
}
if (string.IsNullOrEmpty(myTeacherContactTable.FaxNumber))
{
    myCommand.Parameters.AddWithValue("@FaxNumber", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@FaxNumber", myTeacherContactTable.FaxNumber);
}
if (string.IsNullOrEmpty(myTeacherContactTable.EmailAddress))
{
myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emailAddress", myTeacherContactTable.EmailAddress);
}
myCommand.Parameters.AddWithValue("@Status", myTeacherContactTable.Status);

if (myTeacherContactTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTeacherContactTable.CreatedDateTime);
}
if (myTeacherContactTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTeacherContactTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TeacherContactTable from the database.
/// </summary>
/// <param name="teacherContactKey">The TeacherContactKey of the TeacherContactTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int teacherContactKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherContactTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@teacherContactKey", teacherContactKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TeacherContactTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TeacherContactTable FillDataRecord(IDataRecord myDataRecord )
{
TeacherContactTable myTeacherContactTable = new TeacherContactTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherContactKey")))
{
myTeacherContactTable.TeacherContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherContactKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myTeacherContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
{
myTeacherContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
{
myTeacherContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
{
myTeacherContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OfficePhoneNumber")))
{
    myTeacherContactTable.OfficePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OfficePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
{
    myTeacherContactTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
{
myTeacherContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTeacherContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTeacherContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTeacherContactTable;
}


}

 } 
